//
//  JFLoginViewController.m
//  JiFengDaRen
//
//  Created by zhuchao on 16/7/2.
//  Copyright © 2016年 SH_iOS. All rights reserved.
//

//
//  JFLoginViewController.m
//  JiFengDaRen
//
//  Created by zhuchao on 16/3/22.
//  Copyright © 2016年 SH_iOS. All rights reserved.
//
#import "UserModel.h"
#import "JFLoginViewController.h"
#import "ForgetPswViewController.h"
#import "SHRegisterViewController.h"
#import "JCLoginViewModel.h"
#import "LoginView.h"


@interface JFLoginViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) LoginView * loginView;

@end

@implementation JFLoginViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark - 设置状态栏字体颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return  UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];

    
    self.loginView = [[LoginView alloc] initWithFrame:self.view.frame];
    
    self.loginView.accountField.delegate = self;
    self.loginView.passwordField.delegate = self;
    
    [self.view addSubview:self.loginView];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerAction) name:@"register" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(signAction) name:@"sign" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(forgetAction) name:@"forget" object:nil];
    self.view.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    
    [self.view addGestureRecognizer:singleTap];
    
}


//  进入注册界面
- (void)registerAction{
    
    [self.navigationController pushViewController:[[SHRegisterViewController alloc]init] animated:YES];
}


// 进入忘记密码界面
- (void)forgetAction{
    ForgetPswViewController * pswVC = [[ForgetPswViewController alloc] init];
    [self.navigationController pushViewController:pswVC animated:YES];
}

// 进入登录界面
- (void)signAction{
    
    [self.view endEditing:YES];

    if (self.loginView.accountField.text.length == 0) {
        [self.view makeToast:@"请输入手机号码" duration:2 position:@"center"];
        
        return;
    }
    if (self.loginView.passwordField.text.length == 0) {
        [self.view makeToast:@"请输入登录密码" duration:2 position:@"center"];
        return;
    }
    
    [self DoRequsetLogin];

}


#pragma mark - 网络请求
- (void)DoRequsetLogin{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setValue:self.loginView.accountField.text forKey:@"phone"];
    [params setValue:self.loginView.passwordField.text forKey:@"password"];
    
    [JCLoginViewModel userLoginParams:params Success:^(id result) {

        if ([result[@"success"] boolValue] == YES) {

            [LFUserDefaults setObject:self.loginView.accountField.text forKey:@"ID"];
            [LFUserDefaults setObject:self.loginView.passwordField.text forKey:@"PSW"];
            //友盟统计账号
            NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL: [NSURL URLWithString:[BASE_URL stringByAppendingString:Klogin]]];
            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:cookies];
            [Tool setObject:data forKey:KLoginStatusCookie];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [MBProgressHUD hideHUDForView:self.view animated:YES];

                CYLTabBarController *rootViewController =  [[[JFTabbarControllerConfig alloc]init] tabBarController];
                [UIApplication sharedApplication].keyWindow.rootViewController =  rootViewController;
            
            });
            
            NSNotification *refreshNotice = [[NSNotification alloc]initWithName:@"refreshNotice" object:nil userInfo:nil];
            [[NSNotificationCenter defaultCenter] postNotification:refreshNotice];
            
        }else{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.view makeToast:result[@"data"] duration:2 position:@"center"];
        }
        
    } Failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
    
}

-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer{
    [self.view endEditing:YES];
}


// 销毁通知
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}




@end
